package com.ych.modules.cms.authorInfc.role.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.cms.authorInfc.role.service.RoleService;
import com.ych.modules.base.dbEntity.TSysRoleEntity;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/4
 * Time: 17:03
 * Description:
 */
@RestController("cmsRoleController")
@RequestMapping("/cms/auth/role")
@Api(tags = {"CMS——角色相关"}, description = "CMS——角色相关")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequiresPermissions("cms:auth:role")
    @ApiOperation(value = "查询自己创建角色列表", position = 1)
    @PostMapping("/list")
    public R<Page<TSysRoleEntity>> list(@RequestBody TSysRoleEntity role) {
        Page<TSysRoleEntity> page = roleService.list(role);
        return new R(0, "成功", new PageUtils(page));
    }

    @ApiOperation(value = "查询自己拥有角色列表", position = 2)
    @PostMapping("/select")
    public R<List<TSysRoleEntity>> select() {
        List<TSysRoleEntity> tSysRoleEntities = roleService.select();
        return new R(0, "成功", tSysRoleEntities);
    }

    @RequiresPermissions("cms:auth:role")
    @ApiOperation(value = "角色信息", position = 3)
    @PostMapping("/info")
    public R<TSysRoleEntity> info(@RequestBody String roleStr) {
        TSysRoleEntity tSysRoleEntity = roleService.info(roleStr);
        return new R(0, "查询成功", tSysRoleEntity);
    }

    @RequiresPermissions("cms:auth:role")
    @ApiOperation(value = "保存角色", position = 4)
    @PostMapping("/save")
    public R<String> save(@RequestBody TSysRoleEntity role) {
        roleService.save(role);
        return new R(0, "添加成功", null);
    }

    @RequiresPermissions("cms:auth:role")
    @ApiOperation(value = "修改角色", position = 5)
    @PostMapping("/update")
    public R<String> update(@RequestBody TSysRoleEntity role) {
        roleService.update(role);
        return new R(0, "修改成功", null);
    }

    @RequiresPermissions("cms:auth:role")
    @ApiOperation(value = "删除角色", position = 6)
    @PostMapping("/delete")
    public R<String> delete(@RequestBody String[] roleIdsStr) {
        roleService.delete(roleIdsStr);
        return new R(0, "删除成功", null);
    }
}
